﻿Imports System.Data.SqlClient
Imports System.Data
Imports Microsoft.SqlServer.Server.SqlMetaData
Imports System.Speech.Synthesis
Imports System.IO

'Imports CLASE.frmConfirmacion
Imports CLASE.Datos
Public Class frmTikect

    Dim botones As New Hashtable()
    Dim c As Integer
    Dim i As Integer
    'Dim ges As String = "Gestión "
    Dim voz As New SpeechSynthesizer
    Dim sec As Integer

    Public Sub actualizar()

        If connet.State = ConnectionState.Open Then
            connet.Close()
        End If

        connet.Open()
        Dim nombre As String = "sp_cantidad"
        ''declaramos una variable para establecer que lo que se ejecutara es un  SP
        Dim command As New SqlCommand(nombre, connet)
        command.CommandType = CommandType.StoredProcedure
        '' La variable escalar i almacena el valor que genera el SP y se ejecuta por medio de la insteruccion command.ExecueScalar
        i = command.ExecuteScalar


        For c = 1 To i Step 1
            ' este ciclo imprime la cantida gestiones asignadas representandolas en un boton
            'botones es un arreglo ges= a la palabra Gestiones  y se concatena con la posicion
            botones(c) = c
        Next
        'por medio de ésta consulta se le pueden asigna el idgestion,codgestion y nombre de la gestion a las propiedades, tabindex,name,text

        Dim consulta As String = "Select g.CodGestion,g.NombreGestion  from  GestionCaja gc inner join Gestion g  on  gc.CodGestion=g.codgestion  group by g.NombreGestion,g.CodGestion"

        Dim comando As New SqlCommand(consulta, connet)
        Dim lector As SqlDataReader = comando.ExecuteReader
        If lector.HasRows Then
            ' este ciclo imprime los botones y sus propiedades correspondientes
            While lector.Read

                Dim btn As New Button

                btn.BackColor = System.Drawing.Color.DarkCyan

                btn.Font = New Font("Microsoft Sans Serif", 21.0!)
                btn.Name = lector.GetValue(0).ToString
                btn.Size = New System.Drawing.Size(299, 133)
                btn.Text = lector.GetValue(1).ToString


                'agregar el botton al flowlayout
                flpContenedor.Controls.Add(btn)
                'el boton se agrega al evento click

                AddHandler btn.Click, AddressOf eventosClick


            End While


        End If


        connet.Close()

    End Sub

    Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Try

            ' frmBienvenidos.Hide()

            'Centrar el Panel
            Dim desktopSize As Size = System.Windows.Forms.SystemInformation.PrimaryMonitorSize

            'Captura el Tamaño del Monitor
            Dim ancho As Int32 = (Me.Width - Panel1.Width)
            Panel1.Location = New Point(ancho, Panel1.Location.Y)
            lblfecha.Text = DateTime.Now.ToShortDateString()
            lblhora.Text = DateTime.Now.ToLongTimeString()

            REM se comprueba la coneccion por medio de esta línea de código
            If connet.State = ConnectionState.Open Then

                connet.Close()

            End If
            connet.Open()
            actualizar()
            ' el procedimiento almacenado cantidad retorna un valor escalar que permite la incrementacion o disminucion de la cantidad de botones
            'que apareceran en la pantalla



            'Hashtable es la clase para auto agregar botones
            connet.Close()

        Catch ex As Exception
            'en caso de erro, mostrara un msj al usuario en lugar de que el sistema reviente
            MsgBox(ex.Message)

        End Try

    End Sub

    Protected Sub eventosClick(sender As Object, e As EventArgs)

        Try


            If MsgBox("¿Está seguro de su elección?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "CONFIRMAR") = MsgBoxResult.Yes Then

                Dim boton As Button = TryCast(sender, Button)
                union.sp_Secuencia(boton.Name, sec)
                Dim t As New GestionEnCola With
                    {.CodGestion = boton.Name,
                     .FechaPeticion = Date.Now,
                     .Secuencia = sec,
                     .IdEstado = 1
                    }

                union.GestionEnCola.InsertOnSubmit(t)
                union.SubmitChanges()

                ticket = String.Format("Ticket  {0}   {1}   {2} ", boton.Name, t.FechaPeticion, sec)
                'ticket = "Ticket creado!: " & t.IdGestionEnCola
                MsgBox(ticket, MsgBoxStyle.Information)
                Datos.CargarTicket()
                Datos.Show()
            End If

        Catch ex As Exception
            MsgBox(ex.Message)

        End Try

    End Sub


    Private Sub btncerrar_Click_1(sender As Object, e As EventArgs)
        If MsgBox("¿Estas seguro que desea Salir?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "◄ AVISO ►") = MsgBoxResult.Yes Then
            End

        End If
    End Sub

    Private Sub btnmax_Click_1(sender As Object, e As EventArgs) Handles btnmax.Click
        Me.WindowState = FormWindowState.Normal
    End Sub

    Private Sub btnmin_Click_1(sender As Object, e As EventArgs)
        WindowState = FormWindowState.Minimized
    End Sub


    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        lblhora.Text = DateTime.Now.ToLongTimeString()
    End Sub

    Private Sub btnmin_Click(sender As Object, e As EventArgs) Handles btnmin.Click
        Me.WindowState = FormWindowState.Minimized
    End Sub

    Private Sub btncerrar_Click(sender As Object, e As EventArgs) Handles btncerrar.Click
        If MsgBox("¿Estas seguro que desea Salir?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "◄ AVISO ►") = MsgBoxResult.Yes Then
            End
        End If
    End Sub

    Private Sub Btnactulizar_Click(sender As Object, e As EventArgs) Handles Btnactulizar.Click
        Me.flpContenedor.Controls.Clear()
        actualizar()
    End Sub
End Class
